<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>成绩统计</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="display: none;">
            <div class="layui-form-item">
                <div class="layui-inline">学生账号</div>
                <div class="layui-inline">
                    <input type="text" name="stu_username" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-inline">学生姓名</div>
                <div class="layui-inline">
                    <input type="text" name="stu_realname" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div >

        <div class="layui-card-body">

            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
            <script type="text/html" id="showGrade">
                {{#  if(d.sho_grade <=0){ }}
                    <p style="color: chocolate;">未打分</p>
                {{#  } else { }}
                    {{d.sho_grade}}
                {{#  } }}
            </script>
        </div>
    </div>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use('table', function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;

        //管理员管理
        table.render({
            elem: '#LAY-user-back-manage'
            , url: "{:url ('admin/student/studentsGrade')}?hom_id={$hom_id}&XDEBUG_SESSION_START=10860"
            , cols: [[
                {field: 'index', width: 80, title: 'ID', sort: true, align: 'center'}
                , {field: 'stu_username', title: '用户名', align: 'center',width:120}
                , {field: 'stu_realname', title: '姓名', align: 'center',width:120}
                , {field: 'sho_grade', title: '作业成绩', align: 'center', sort: true, edit: "text",width:120}
                , {field: 'sho_remark', title: '作业评语', align: 'center',  edit: "text"}
            ]]
            , text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-user-back-manage', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    'stu_username': field.stu_username,
                    "stu_realname": field.stu_realname
                }
            });
        });

        //监听单元格编辑
        table.on('edit(LAY-user-back-manage)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data; //得到所在行所有键值
            var sho_grade=data.sho_grade;
            var sho_remark=data.sho_remark;

            if(isNaN(sho_grade)){
                layer.msg("请输入数字", {icon: 5, time: 500}, function () {
                });
                return false;
            }
            //{sho_id: 1, sho_stu_id: 1, sho_hom_id: 1, sho_grade: "123", sho_create_time: "2019-07-09 18:53:47", …}
            var postData={
                "sho_id":data.sho_id,
                "newValue":sho_grade,
                "sho_remark":sho_remark
            };
            $.ajax({
                url: "{:url ('admin/student/studentChangeGrade')}",
                type: "post",
                dataType: "json",
                data:postData,
                success: function (res) {
                    if (res.code == 1) {
                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                            obj.del();
                        });
                    } else {
                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                        });
                    }
                }
            })

        });

        //监听工具条
        table.on('tool(LAY-user-back-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除此学生？', function (index) {
                    //获取当前管理员的id
                    var stu_id = data.stu_id;
                    $.ajax({
                        url: "{:url ('admin/student/studentDel')}?stu_id=" + stu_id,
                        type: "post",
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 1) {
                                layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                    obj.del();
                                });
                            } else {
                                layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                });
                            }
                        }
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                var stu_id = data.stu_id;
                layer.open({
                    type: 2
                    , title: '编辑学生'
                    , content: "{:url ('admin/student/studentEdit')}?stu_id=" + stu_id
                    , area: ['800px', '370px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-student-edit-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            $.ajax({
                                url: "{:url ('admin/student/studentEdit')}?stu_id=" + stu_id,
                                type: "post",
                                dataType: "json",
                                data: field,
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                            window.location.reload();
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                            // window.location.reload();
                                        });
                                    }
                                }
                            })
                            // table.reload('LAY-user-back-manage'); //数据刷新
                            layer.close(index); //关闭弹层
                        });
                        submit.trigger('click');
                    }
                    , success: function (layero, index) {

                    }
                })
            }
        });

        //事件(table上面的添加功能)
        var active = {
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加学生'
                    , content: "{:url ('admin/student/studentAdd')}"
                    , area: ['800px', '370px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-student-add-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //field:
                            //adm_mobile: "12312312312"
                            // adm_password: "123"
                            // adm_realname: "123"
                            // adm_role_id: "2"
                            // adm_status: "on"
                            // adm_username: "123"
                            // email: "123@qq.com"
                            // repass: "123"
                            //拿到数据后进行ajax提交数据
                            //发起ajax请求
                            $.ajax({
                                url: "{:url ('admin/student/studentAdd')}",
                                type: "post",
                                data: field,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                            window.location.reload();
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                        });
                                    }
                                }
                            })
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

